package com.jk.dmuserprovider.service;

import com.jk.dmcommon.entity.OrderEntity;
import com.jk.dmcommon.entity.ShippingEntity;
import com.jk.dmcommon.entity.CouponEntity;
import com.jk.dmcommon.entity.TreeEntity;
import com.jk.dmcommon.entity.LeaveEntity;
import com.jk.dmcommon.entity.SessionEntity;
import com.jk.dmuserprovider.dao.TreeDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.jk.dmcommon.LybEntity.UserEntity;

import java.util.List;

/**
 * @author 赵优秀
 * @version 1.0.0
 * @ClassName TreeService.java
 * @Description TODO
 * @createTime 2021年09月07日 11:30:00
 */
@Service
public class TreeService {
    @Autowired
    private TreeDao treeDao;
    @Autowired
    private MongoTemplate mongoTemplate;
    //redis
    @Autowired
    private RedisTemplate redisTemplate;
//树
    public List<TreeEntity> getTree(Integer pid) {
        List<TreeEntity> list=treeDao.getTree(pid);
        for (TreeEntity tree: list) {
            tree.setNodes(getTree(tree.getId()));
        }
        return list;
    }
    /**
     * @title 回显
     * @description
     * @author 赵优秀
     * @updateTime 2021/9/7 19:45
     * @throws
     * @return
     */
    public UserEntity getByid() {
        UserEntity user = (UserEntity) redisTemplate.opsForHash().get("dmLoginInfo","loginInfo");
        return user;
    }
    /**
     * @title 修改
     * @description 
     * @author 赵优秀 
     * @updateTime 2021/9/7 20:49 
     * @throws 
     */
    public String updateUser(UserEntity user) {
        treeDao.updateUser(user);
        String msg="保存成功";
        return msg;
    }
    /**
     * @title 查询收货地址
     * @description
     * @author 赵优秀
     * @updateTime 2021/9/8 14:08
     * @throws
     */
    public List<ShippingEntity> findAddress() {
        UserEntity user = (UserEntity) redisTemplate.opsForHash().get("dmLoginInfo","loginInfo");

        return treeDao.findAddress(user.getUserId());
    }
    /**
     * @title 新增收货地址
     * @description
     * @author 赵优秀
     * @updateTime 2021/9/8 15:47
     * @throws
     */
    public void addAddress(ShippingEntity shipping) {
        if (shipping.getShippingaddressId()==null){
            UserEntity user = (UserEntity) redisTemplate.opsForHash().get("dmLoginInfo","loginInfo");

            treeDao.addAddress(shipping);
        }else {
            treeDao.updateAddress(shipping);
        }

    }
    /**
     * @title 删除地址
     * @description
     * @author 赵优秀
     * @updateTime 2021/9/8 16:30
     * @throws
     */
    public void delAddress(Integer id) {
        treeDao.delAddress(id);
    }
    /**
     * @title 回显地址
     * @description
     * @author 赵优秀
     * @updateTime 2021/9/8 16:49
     * @throws
     */
    public ShippingEntity getAddress(Integer id) {
        return treeDao.getAddress(id);
    }
    /**
     * @title 查询优惠券
     * @description
     * @author 赵优秀
     * @updateTime 2021/9/8 19:14
     * @throws
     */
    public List<CouponEntity> findCoupon(CouponEntity coupon) {
        UserEntity user = (UserEntity) redisTemplate.opsForHash().get("dmLoginInfo","loginInfo");
        coupon.setUserId(user.getUserId());
        List<CouponEntity> list= treeDao.findCoupon(coupon);

        return list;
    }
    /**
     * @title 查询订单
     * @description
     * @author 赵优秀
     * @updateTime 2021/9/8 21:29
     * @throws
     */
    public List<OrderEntity> findOrder(OrderEntity orderEntity) {
        UserEntity user = (UserEntity) redisTemplate.opsForHash().get("dmLoginInfo","loginInfo");
        Query query=new Query();
        query.addCriteria(Criteria.where("buyUserId").is(user.getUserId()));
        List<OrderEntity> list = mongoTemplate.find(query, OrderEntity.class);
        return list;
    }
    /**
     * @title 查询场次
     * @description
     * @author 赵优秀
     * @updateTime 2021/9/9 14:41
     * @throws
     * @param goodsId
     */
    public List<SessionEntity> getSession(Integer goodsId) {
        return treeDao.getSession(goodsId);
    }
    /**
     * @title 获取档次
     * @description
     * @author 赵优秀
     * @updateTime 2021/9/9 15:37
     * @throws
     */
    public List<LeaveEntity> getLeave(Integer id) {
        return treeDao.getLeave(id);
    }
}
